package com.changan.controller;

import com.changan.entity.Classes;
import com.changan.entity.Student;
import com.changan.service.ClassesService;
import com.changan.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @program: StuManagement
 * @description: StudentController控制器类
 * @author: Mr.Nie
 * @create: 2021-12-18 13:58
 **/
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassesService classesService;

    @RequestMapping("/getStudents")
    public String getStudents(@RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize",required = true,defaultValue = "2") Integer pageSize,
                              Model model){
        PageInfo<Student> pageInfo = studentService.getStuByPage(pageNum,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "index";
    }

    @RequestMapping("/toAddStu")
    public String toAddStu(Model model){
        List<Classes> classes = classesService.getClasses();
        model.addAttribute("classes",classes);
        return "add";
    }

    @RequestMapping("/addStu")
    public String addStu(Student student){
        int i = studentService.addStudent(student);
        //添加成功之后重定向到查询所有的学生信息
        return "redirect:/getStudents";
    }

    @RequestMapping("/delStu")
    public String delStu(Long id){
        int i = studentService.delStu(id);
        return "redirect:/getStudents";
    }

    @RequestMapping("/updStu")
    public String updStu(Student student){
        int i = studentService.updStu(student);
        return "redirect:/getStudents";
    }

    @RequestMapping("/toUpdStu")
    public String toUpdStu(Long id,Model model){
        Student student = studentService.getStudentById(id);
        List<Classes> classes = classesService.getClasses();
        model.addAttribute("classes",classes);
        model.addAttribute("student",student);
        return "edit";
    }
}
